﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MDK.Common
{
    public class SampleResultApi
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        static string _staticurl = "http://198.190.100.247/shinow_app/RestService.asmx/MBS_ShareQueryApi/Api";
        static string _strToken;
        private static TimeSpan _handoverTimeout = TimeSpan.FromSeconds(600);
        static DateTime _timestamp = DateTime.Now;
        
        static string Token { get {
                if (string.IsNullOrEmpty(_strToken) || DateTime.Now - _timestamp > _handoverTimeout)
                {
                    _timestamp = DateTime.Now;
                    _strToken = GetToken("MDKJK", "111");
                }
                return _strToken;
            }
        }
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPassword"></param>
        static string GetToken(string UserName,string UserPassword)
        {
            try
            {
                string Result = DESEncrypt_TS.ToDESDecrypt(HttpPost($"{_staticurl}/Token", DESEncrypt_TS.ToDESEncrypt(JsonConvert.SerializeObject(new { UserName = UserName, UserPassword = UserPassword }))));
                var data = JsonConvert.DeserializeObject<TokenResultData>(Result);
                return data.Data;
            }
            catch(Exception e)
            {
                return "";
            }
          
        }
        public static PstABOData GetDonorInfo(string DonCode)
        {
            try
            {

                WriteTxt.WriteCode(DonCode);
                string Result = DESEncrypt_TS.ToDESDecrypt(HttpPost($"{_staticurl}/MaiDiKeSampleQuery?token={System.Web.HttpUtility.UrlEncode(Token)}", DESEncrypt_TS.ToDESEncrypt(JsonConvert.SerializeObject(new { DonCode = DonCode }))));
                var data = JsonConvert.DeserializeObject<ResultData>(Result);
                if (!data.IsSuccess)
                    return new PstABOData();

                return data.Data;
            }
            catch (Exception e)
            {
                return new PstABOData();
            }
        }
        /// <summary>
        /// Post提交数据请求
        /// </summary>
        /// <param name="POSTURL">请求提交的地址</param>
        /// <param name="PostData">提交的数据(字符串)</param>
        /// <returns></returns>
        static string HttpPost(string POSTURL, string PostData)
        {
            //发送请求的数据
            WebRequest myHttpWebRequest = WebRequest.Create(POSTURL);
            myHttpWebRequest.Method = "POST";
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byte1 = encoding.GetBytes(PostData);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            myHttpWebRequest.ContentLength = byte1.Length;
            Stream newStream = myHttpWebRequest.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();

            HttpWebResponse response;
            try
            {
                //发送成功后接收返回的XML信息
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
            string lcHtml = string.Empty;
            Encoding enc = Encoding.GetEncoding("UTF-8");
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, enc);
            lcHtml = streamReader.ReadToEnd();
            return lcHtml;
        }
    }
    public class TokenResultData
    {
        public bool IsSuccess { get; set; }
        public string Data { get; set; }
        public string ErrorMessage { get; set; }
    }
    public class ResultData
    {
        public bool IsSuccess { get; set; }
        public PstABOData Data { get; set; }
        public string ErrorMessage { get; set; }
    }
    public class PstABOData
    {
        public string PstABO { get; set; }
        public DateTime CollectDate { get; set; }
    }
}
